/**
 * @author lwlianghehe@gmail.com
 * @date 2024/12/16 20:33
 */
import xmlFormatter from "xml-formatter";
import { ParserField } from "../xml/model/ParserField";

export function formatXML(xmlString: string): string {
    try {
        // 格式化 XML 字符串
        const formattedXML = xmlFormatter(xmlString, {
            indentation: "  ", // 使用两个空格缩进
            collapseContent: true, // 是否折叠内容（比如将单行内容保持在同一行）
            lineSeparator: "\n", // 换行符
        });
        return formattedXML;
    } catch (error) {
        // 捕获格式化错误并返回原始 XML 字符串
        console.error("Error formatting XML:", error);
        return xmlString;
    }
}

export function getWidget(field: ParserField): string | undefined {
    if (field.widget) {
        if (field.widget == 'xml') {
            return `MyXmlViewer`
        } else if (field.widget == 'chat') {
            return `ChatWindow`
        } else if (field.widget == 'document') {
            return `DocumentList`
        } else if (field.widget == 'address') {
            return `MyAddress`
        } else if (field.widget == 'markdown') {
            return `Markdown`
        } else if (field.widget == 'json') {
            return "MyJson"
        } else if (field.widget == 'text') {
            return `MyTextarea`
        } else if (field.widget == 'color') {
            return `ColorPicker`
        } else {
            return field.widget
        }
    }
}


// 示例用法
// const xmlString = `<root><child>value</child><child2><nested>text</nested></child2></root>`;
// const formattedXML = formatXML(xmlString);